﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.HomeServer.SDK.Interop.v1;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace Umbrella.Whs.SharePoint
{
    public sealed class AlternateAccessMappingManager : SharePointConnected
    {
        private readonly log4net.ILog _log = log4net.LogManager.GetLogger(typeof(AlternateAccessMappingManager));
        private WHSInfo _info;

        public void UpdateMapping()
        {
            _log.Info("Updating SharePoint alternate access mapping...");

            try
            {
                var dn = _info.GetDomainName();

                if (string.IsNullOrEmpty(dn))
                {
                    _log.Info("No domain name set; removing mapping...");

                    // TODO: remove any *.homeserver.com mappings
                    foreach (var mc in this.Farm.AlternateUrlCollections)
                    {
                    }
                }
                else
                {
                    _log.Info("Domain name: " + dn + " ; updating mapping...");

                    // TODO: update mapping
                }

                _log.Info("Updated SharePoint alternate access mapping");

            }
            catch (Exception x)
            {
                _log.Error("Failed to update alternate access mapping", x);

                throw;
            }
        }

        public AlternateAccessMappingManager()
        {
            _info = new WHSInfoClass();

            _info.Init(Whs.Properties.Constants.ApplicationName);
        }
    }
}
